import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import axios from 'axios'
axios.defaults.baseURL = "http://39.108.135.214:8899/"
axios.defaults.withCredentials = true
Vue.prototype.$axios = axios

//导入组件
import layout from '../components/layout'
import goodslist from '../components/goods/goodslist'
import goodsinfo from '../components/goods/goodsinfo'
import shopcart from '../components/shopcart/shopcart'
import order from '../components/order/order'
import login from '../components/account/login'
import payOrder from '../components/pay/payOrder'
import vipCenter from '../components/vip/vipCenter'
import myOrders from '../components/vip/myOrders'
import pcPaySuccess from '../components/pay/pcPaySuccess'
import orderInfo from '../components/vip/orderInfo'

var router = new VueRouter({
    routes:[
        {path:'/',redirect:'/site/goodslist'},
        {path:'/site',component:layout,children:[
            {path:'goodslist',component:goodslist},
            {path:'goodsinfo/:goodsId',component:goodsinfo},
            {path:'shopcart',component:shopcart},
            {path:'login',component:login},
            {path:'order/:ids',component:order,meta:{needLogin:true}},
            {path:'payOrder/:orderid',component:payOrder,meta:{needLogin:true}},
            {path:'vipCenter',component:vipCenter,meta:{needLogin:true}},
            {path:'myOrders',component:myOrders,meta:{needLogin:true}},
            {path:'pcPaySuccess',component:pcPaySuccess,meta:{needLogin:true}},
            {path:'orderInfo/:orderid',component:orderInfo,meta:{needLogin:true}}
        ]}
    ]
})

//全局路由守卫
router.beforeEach((to, from, next) => {
    //记录最后一次访问的路径，方便到时候登录完成之后，跳转回去
    if(to.path!='/site/login'){
        localStorage.setItem('lastVisitPath',to.path)
    }

    //检测是否有权限跳转
    if(to.meta.needLogin){//需要登录的组件
        axios.get('site/account/islogin').then(response=>{
            if(response.data.code=='nologin'){
                router.push('/site/login')
            }else{
                //检测是否
                next()
            }
        })
    }else{
        //检测是否
        next()
    }
})

export default router